<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--用户数据-->
      <el-col :span="24" :xs="24">
        <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="关键字" prop="userName">
            <el-input
                v-model="queryParams.userName"
                placeholder="请输入关键字"
                clearable
                style="width: 240px"
                @keyup.enter="handleQuery"
            />
          </el-form-item>
          <el-form-item label="审核状态" prop="status">
            <el-select
                v-model="queryParams.status"
                placeholder="审核状态"
                clearable
                style="width: 240px"
            >
              <el-option
                  v-for="dict in sys_normal_disable"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="创建时间" style="width: 308px;">
            <el-date-picker
                v-model="dateRange"
                value-format="YYYY-MM-DD"
                type="daterange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
            ></el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
                type="primary"
                plain
                :disabled="multiple"
                @click="handleDelete"
                v-hasPermi="['system:user:remove']"
            >删除
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
                type="primary"
                plain
                :disabled="multiple"
                @click="handleYes"
                v-hasPermi="['system:user:remove']"
            >批量通过
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
                type="primary"
                plain
                :disabled="multiple"
                @click="handleNo"
                v-hasPermi="['system:user:remove']"
            >批量驳回
            </el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="50" align="center"/>
          <el-table-column label="用户编号" align="center" key="userId" prop="userId"/>
          <el-table-column label="用户角色" align="center" key="roleName" prop="roleName" :show-overflow-tooltip="true"/>
          <el-table-column label="所属单位" align="center" key="affiliatedUnit" prop="affiliatedUnit" :show-overflow-tooltip="true"/>
          <el-table-column label="审核状态" align="center" key="checkStatusLabel" prop="checkStatusLabel" width="120" :show-overflow-tooltip="true"/>
          <el-table-column label="姓名" align="center" key="trueName" prop="trueName" width="120"/>
          <el-table-column label="联系电话" align="center" key="phoneNumber" prop="phoneNumber" width="120"/>
          <el-table-column label="组织机构代码" align="center" key="organizationCode" prop="organizationCode" width="120"/>
          <el-table-column label="单位性质" align="center" key="unitNatureLabel" prop="unitNatureLabel" width="120"/>
          <el-table-column label="注册账号" align="center" key="username" prop="username" width="120"/>
          <el-table-column label="注册时间" align="center" prop="createTime" width="160">
            <template #default="scope">
              <span>{{ parseTime(scope.row.createTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="审核时间" align="center" prop="checkTime" width="160">
            <template #default="scope">
              <span>{{ parseTime(scope.row.checkTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" width="150" class-name="small-padding fixed-width" fixed="right">
            <template #default="scope">
              <el-button link type="primary" @click="handleYes(scope.row)" v-hasPermi="['system:user:remove']">通过</el-button>
              <el-button link type="primary" @click="handleNo(scope.row)" v-hasPermi="['system:user:remove']">驳回</el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination
            v-show="total > 0"
            :total="total"
            v-model:page="queryParams.pageNum"
            v-model:limit="queryParams.pageSize"
            @pagination="getList"
        />
      </el-col>
    </el-row>
    <el-dialog title="驳回原因" v-model="openNo" width="500px" append-to-body>
      <el-form ref="formNoRef" :model="formNo" :rules="rules" label-width="80px">
        <el-form-item label="原因简述" prop="reasons">
          <el-input type="textarea" v-model="formNo.reasons" placeholder="请输入项目编号" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitFormNo">确 定</el-button>
          <el-button @click="cancelNo">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import {listAuditUser, auditUser, delUser} from '@/api/departmentWaterResources/massageAudit/user.js'
import {parseTime} from "@/utils/ruoyi.js";
import {useRouter} from "vue-router";

const router = useRouter();
const {proxy} = getCurrentInstance();
const {sys_user_check_status, sys_user_sex} = proxy.useDict("sys_user_check_status", "sys_user_sex");

const userList = ref([]);
const open = ref(false);
const openNo = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const dateRange = ref([]);
const deptName = ref("");
const formNo = ref({})

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    userName: undefined,
    phonenumber: undefined,
    status: undefined,
    deptId: undefined
  },
  rules: {
    userName: [{required: true, message: "用户名称不能为空", trigger: "blur"}, {min: 2, max: 20, message: "用户名称长度必须介于 2 和 20 之间", trigger: "blur"}],
    nickName: [{required: true, message: "用户昵称不能为空", trigger: "blur"}],
    password: [{required: true, message: "用户密码不能为空", trigger: "blur"}, {min: 5, max: 20, message: "用户密码长度必须介于 5 和 20 之间", trigger: "blur"}, {
      pattern: /^[^<>"'|\\]+$/,
      message: "不能包含非法字符：< > \" ' \\\ |",
      trigger: "blur"
    }],
    email: [{type: "email", message: "请输入正确的邮箱地址", trigger: ["blur", "change"]}],
    phoneNumber: [{pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/, message: "请输入正确的手机号码", trigger: "blur"}],
    reasons: [{required: true, message: "原因简述不能为空", trigger: "blur"}]
  }
});

const {queryParams, form, rules} = toRefs(data);

/** 根据名称筛选部门树 */
watch(deptName, val => {
  proxy.$refs["deptTreeRef"].filter(val);
});

/** 查询用户列表 */
function getList() {
  loading.value = true;
  listAuditUser(proxy.addDateRange(queryParams.value, dateRange.value)).then(res => {
    loading.value = false;
    userList.value = res.rows;
    total.value = res.total;
  });
};

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
};

/** 重置按钮操作 */
function resetQuery() {
  dateRange.value = [];
  proxy.resetForm("queryRef");
  queryParams.value.deptId = undefined;
  proxy.$refs.deptTreeRef.setCurrentKey(null);
  handleQuery();
};

/** 删除按钮操作 */
function handleDelete(row) {
  let userIds = row.userId || ids.value;
  if (!Array.isArray(userIds)) userIds = [userIds];
  proxy.$modal.confirm('是否确认删除用户编号为"' + userIds + '"的数据项？').then(function () {
    return delUser(userIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  });
}

function handleYes(row) {
  let userIds = row.userId || ids.value;
  if (!Array.isArray(userIds)) userIds = [userIds];
  proxy.$modal.confirm('是否确认通过用户编号为"' + userIds + '"的数据项？').then(function () {
    return auditUser({userIds: userIds, checkStatus: 1});
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("通过成功");
  }).catch(() => {
  });
}

const delUserRow = ref({})
function handleNo(row) {
  delUserRow.value = row
  openNo.value = true;
}

function submitFormNo() {
  proxy.$refs.formNoRef.validate((valid) => {
    if (valid) {
      let userIds = delUserRow.value?.userId || ids.value;
      if (!Array.isArray(userIds)) userIds = [userIds];
      auditUser({userIds: userIds, checkStatus: 2, reasons: formNo.value.reasons}).then(res => {
        proxy.$modal.msgSuccess("驳回成功");
        openNo.value = false;
        getList();
        delUserRow.value = {}
      })
    }
  })
}

function cancelNo() {
  delUserRow.value = {}
  openNo.value = false;
}

/** 选择条数  */
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.userId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

getList();
</script>
